package priv.lhy.activity.services.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import priv.lhy.activity.dal.entity.ActDrawAward;
import priv.lhy.activity.dal.entity.ActDrawRecord;
import priv.lhy.activity.dal.persistence.ActDrawRecordMapper;
import priv.lhy.activity.services.processor.constants.DrawContants;
import priv.lhy.activity.services.processor.exception.UnrewardException;

/**
 * author : lihy
 * date : 2019/2/16 9:12
 * 抽奖过程抽象父类
 */
public abstract class AbstractRewardProcessor implements IRewardProcessor<ActivityDrawContext> {

    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractRewardProcessor.class);

    @Autowired
    protected RedisTemplate redisTemplate;

    @Autowired
    ActDrawRecordMapper actDrawRecordMapper;

    @Override
    public ActivityDrawContext doReword(ActivityDrawContext activityDrawContext) {
        //验证奖品数量及获取奖品明细
        beforeProcessor(activityDrawContext);

        //处理发放奖品
        processor(activityDrawContext);

        //发送短信
        afterProcessor(activityDrawContext);

        return activityDrawContext;
    }

    /**
     * 保存中奖纪录
     * @param context
     */
    protected void modifyAwardRecord(ActivityDrawContext context){
        ActDrawRecord record = new ActDrawRecord();
        record.setAwardName(context.getActDrawAward().getAwardName());
        record.setLevel(context.getActDrawAwardItem().getLevel());
        record.setMobile(context.getCurrenUser().getMobile());
        record.setUid(context.getActivityTurntableDrawReq().getUid());
        record.setName(context.getCurrenUser().getRealName());

        actDrawRecordMapper.addActDrawRecord(record);
    }

    /**
     * 验证奖品信息
     * @param activityDrawContext
     */
    private void beforeProcessor(ActivityDrawContext activityDrawContext) {
        //判断奖项当天发放总数以及整个活动过程中的总数：total_num、day_total_num
        if (validDayAwardNum(activityDrawContext) && validTotalAwardNum(activityDrawContext)) {
            ActDrawAward actDrawAward = (ActDrawAward) redisTemplate.opsForValue().get(DrawContants.DRAW_AWARD + getAwardType() + ":" + activityDrawContext.getActDrawAwardItem().getAwardId());
            //TODO 判断奖品状态
            activityDrawContext.setActDrawAward(actDrawAward);
        } else {
            throw new UnrewardException("当前奖项已达到当日最大限制");
        }
    }

    /**
     * 校验奖品数量
     * @param activityDrawContext
     * @return
     */
    protected boolean validTotalAwardNum(ActivityDrawContext activityDrawContext) {
        //TODO 判断当前奖项发放总的发放数量
        return true;
    }

    /**
     * 校验每日奖品数量
     * @param activityDrawContext
     * @return
     */
    protected boolean validDayAwardNum(ActivityDrawContext activityDrawContext) {
        //TODO 判断当天针对当前奖项的数量
        return true;
    }

    /**
     * 抽奖结束后的操作
     * @param activityDrawContext
     */
    protected abstract void afterProcessor(ActivityDrawContext activityDrawContext);

    /**
     * 发放对应的奖品
     * @param activityDrawContext
     */
    protected abstract void processor(ActivityDrawContext activityDrawContext);

    /**
     * 返回当前奖品类型
     *
     * @return
     */
    protected abstract int getAwardType();
}
